var app = getApp();
Page({
    data:{
        address : '',
        siteroot : app.siteInfo.siteroot.replace('app/index.php', ''),uniacid: app.siteInfo.uniacid,
        formdata : {},
    },
    onLoad:function(options){
        var self = this;
        var zfid = options.zfid ? options.zfid : ( options.scene ? options.scene : 0 );
        self.setData({
            options : options,
        });
        var isposter = options.scene  ? 1 : 0;
        isposter = options.isposter*1 == 1 ? 1 : isposter;
        
        app.com.auth(function(user){
            app.com.http('user_v1','POST',{zfid:zfid,op:'joinfx',isposter:isposter},0,false,'','',function(res){
                if (!res.data.errno) {
                    self.setData({
                        params : res.data.data.params,
                        inited : true,
                    });
                }else{
                    app.com.alert(res.data.message);
                }
            });
        });

        wx.setNavigationBarTitle({
          title:  '提交申请',
        });
    },
    submit(e){
        var self = this;
        var postdata = {
            op : 'subjoinfx',
            name : e.detail.value.name ? e.detail.value.name : '',
            tel : e.detail.value.phone ? e.detail.value.phone : '',
            fxcode : e.detail.value.fxcode ? e.detail.value.fxcode : '',
            ctp : self.data.address[0] ? self.data.address[0] : '',
            ctc : self.data.address[1] ? self.data.address[1] : '',
            ctd : self.data.address[2] ? self.data.address[2] : '',
        };
        if( postdata.name == '' ){
            app.com.alert('请填写姓名');
            return false;
        }
        if( postdata.tel == '' ){
            app.com.alert('请填写电话');
            return false;
        }
        if( !postdata.ctp && self.data.params.ctyhx == 1){
            app.com.alert('请选择地区');
            return false;
        }

        if(self.data.params.needauth == 1){
            self.loginauth = this.selectComponent('#loginauth');
            self.loginauth.hideauth();
            self.data.formdata = e;
            return false;
        }

        app.com.http('user_v1','POST',postdata,0,true,'','',function(res){
            if (!res.data.errno && res.data ) {
                app.com.alert(res.data.message,function(){
                    wx.navigateBack({delta:2});
                });
            }else{
                app.com.alert(res.data.message);
            }
        });

    },
    authed(e){
        var self = this;
        self.data.params.needauth = 0;
        setTimeout(function(){
            self.submit(self.data.formdata);
        },50)
    },
    bindRegionChange : function(e){
        var self = this;
        self.setData({
            address : e.detail.value
        })
    },
    selectarea(){
        this.selectComponent('#ausarea').show();
    },
    location : function( e ){
        app.com.location( e );
    },
    otherapp : function( e ){
        app.com.otherapp( e );
    },
    navigateto : function( e ){
        app.com.navigateto( e );
    },
    redirectto : function( e ){
        app.com.redirectto( e );
    },    
    callphone : function(e){
        app.com.callphone( e );
    },
    toweburl : function(e){
        app.com.toweburl( e );
    },
    showimages : function( e ){
        app.com.showimages( e );
    },
    footNavigate(e){
        var url = e.currentTarget.dataset.url;
        wx.switchTab({
          url: url
        })
    },

})